﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessModel;
using BusinessModel.EntityModel;
using System.Drawing;


namespace SDZJ.ManagerView.InfomationManager
{
    public partial class InfoTypeManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.grvDataBind();
            }
            if (Request.QueryString["InfoCode"] != null)
            {
                string infocode = Request.QueryString["InfoCode"].ToString();
                string infotype = Request.QueryString["Infotype"].ToString();
                NewsType newsType = new NewsType();
                newsType.newstypecode = infocode;
                newsType.newstype = infotype;
                InfoHelper InfoHelper = new InfoHelper();
                string message= InfoHelper.AddNewsType(newsType);
                //this.grvDataBind();
                Response.Write(message);
                Response.End();
            }
        }
        
        //绑定GRIDVIEW数据
        private void grvDataBind()
        {
            EntityOprHelper entityHelper = new EntityOprHelper();
            List<NewsType> newsType = entityHelper.GetEntityList<NewsType>("");
            // List<MaterialListInfo> newsType = entityHelper.GetEntityList<MaterialListInfo>("");
            this.grvNewsType.DataSource = newsType;
            this.grvNewsType.DataBind();
        }

        protected void grvNewsType_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#f4fff7'");
                //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                //e.Row.Attributes["style"] = "cursor:hand";

                //鼠标移动到某行上，该行变色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#f3f3f3'");

                //鼠标移开后，恢复
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                //点击后，实现该行的选中                                                              

                e.Row.Attributes.Add("onclick", "__doPostBack('grvNewsType','Select$" + e.Row.RowIndex + "')");

                e.Row.Attributes.Add("ondblclick", "showeditdiv()");


            }

        }

        //protected void elbtnDelete_Command(object sender, CommandEventArgs e)
        //{
        //    try
        //    {
        //        EntityOprHelper entityHelper = new EntityOprHelper();
        //        NewsType newsinfo=entityHelper.GetEntity<NewsType>(e.CommandName);
        //        entityHelper.DelEntity<NewsType>(newsinfo);
        //        this.grvDataBind();
        //    }
        //    catch (Exception ex)
        //    {
                
        //    }
        //}

        //protected void elbtnEdit_Command(object sender, CommandEventArgs e)
        //{
        //    try
        //    {
        //        EntityOprHelper entityHelper = new EntityOprHelper();
        //        NewsType newsinfo = entityHelper.GetEntity<NewsType>(e.CommandName);
        //        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "editdiv('" + newsinfo.newstypecode + "','" + newsinfo.newstype + "');", true);
        //        //entityHelper.EditEntity<NewsType>(newsinfo);
        //        //this.grvDataBind();
        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //}

        //添加信息
        protected void btnOK_Click(object sender, EventArgs e)
        {
            NewsType newsType = new NewsType();
            newsType.newstypecode = this.txtCode.Text;
            newsType.newstype = this.txtType.Text;
            InfoHelper InfoHelper = new InfoHelper();
            string message = InfoHelper.AddNewsType(newsType);
            this.grvDataBind();
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "addreuslt('"+message+"');", true);
        }

        //编辑信息
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                EntityOprHelper entityHelper = new EntityOprHelper();
                NewsType newsinfo = new NewsType();
                newsinfo.newstypecode = this.txtCode.Text;
                newsinfo.newstype = this.txtType.Text;
               // ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "editdiv('" + newsinfo.newstypecode + "','" + newsinfo.newstype + "');", true);
                InfoHelper InfoHelper = new InfoHelper();
                string message = InfoHelper.EditNewsType(newsinfo);
                this.grvDataBind();
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "addreuslt('" + message + "');", true);
            }
            catch (Exception ex)
            {

            }
        }

        protected void grvNewsType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //设置选中行的颜色
            this.grvNewsType.SelectedRowStyle.BackColor = Color.FromName("#f3f3f3");
            this.txtId.Text = ((Label)this.grvNewsType.SelectedRow.FindControl("lblNewTypeCode")).Text;
            this.txtValue.Text = ((Label)this.grvNewsType.SelectedRow.FindControl("lblNewType")).Text;
        }

        protected void grvNewsType_RowCreated(object sender, GridViewRowEventArgs e)
        {
            //将GRIDVIEW的第一列，即选择列隐藏
            e.Row.Cells[0].Attributes.Add("style", "display:none;");
        }

        //删除信息类别
        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                EntityOprHelper entityHelper = new EntityOprHelper();
                NewsType newsinfo = entityHelper.GetEntity<NewsType>(this.txtId.Text);
                entityHelper.DelEntity<NewsType>(newsinfo);
                this.grvDataBind();
            }
            catch (Exception ex)
            {

            }
        }
    }
}